package lj.courseinterect.rest.resources;

import lj.courseinterect.dao.i.AnswerInfoDao;
import lj.courseinterect.dao.i.QuestionInfoDao;
import lj.courseinterect.dao.i.StudentDao;
import lj.courseinterect.entity.AnswerInfo;
import lj.courseinterect.entity.QuestionInfo;
import lj.courseinterect.entity.Student;
import lj.courseinterect.transfer.AnswerTransfer;
import lj.courseinterect.transfer.OperationTransfer;
import lj.courseinterect.utils.GoodCount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Component
@Path("/{a:tea|stu}/answers")
public class AnswerInfoResource
{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AnswerInfoDao answerInfoDao;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private StudentResource studentResource;

    @Autowired
    private QuestionInfoDao questionInfoDao;

    public Logger getLogger() {
        return logger;
    }

    public AnswerInfoDao getAnswerInfoDao() {
        return answerInfoDao;
    }

    public void setQuestionInfoDao(AnswerInfoDao answerInfoDao) {
        this.answerInfoDao = answerInfoDao;
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public OperationTransfer<AnswerTransfer> create(AnswerInfo answerInfo)
    {
        this.logger.info("create(): " + answerInfo.getAnsName());
        OperationTransfer<AnswerTransfer> operationTransfer;
        AnswerTransfer answerTransfer = new AnswerTransfer();

        Student student = studentResource.getStudent();
        answerInfo.setAnsTime(new Date());
        answerInfo.setStuId(student.getStuId());
        try {
            if(answerInfo.getAnsId() != null){
                //首次添加　涨积分
                this.updateQuestionGood(answerInfo.getQueId(), GoodCount.ADD_ANSWER);
            }
            answerInfo = this.answerInfoDao.save(answerInfo);


            answerTransfer.setAnswerInfo(answerInfo);
            operationTransfer = new OperationTransfer<AnswerTransfer>(true, "添加回答成功", answerTransfer);
        } catch (Exception ex){
            operationTransfer = new OperationTransfer<AnswerTransfer>(false, "添加回答信息失败", null);
        }

        this.logger.info("create(): " + operationTransfer.toString());
        return operationTransfer;
    }

    @GET
    @Path("/{ansId: [0-9]*$}")
    @Produces(MediaType.APPLICATION_JSON)
    public AnswerTransfer get(@PathParam("ansId") Long ansId)
    {
        AnswerTransfer answerTransfer = new AnswerTransfer();
        AnswerInfo answerInfo = answerInfoDao.find(ansId);
        answerTransfer.setAnswerInfo(answerInfo);
        if(answerInfo != null){
            Student student = studentDao.find(answerInfo.getStuId());
            answerTransfer.setStudent(student);
        }
        return answerTransfer;
    }

    @GET
    @Path("/{ansId}/phrase")
    @Produces(MediaType.APPLICATION_JSON)
    public OperationTransfer<AnswerInfo> phraseAnswer(@PathParam("ansId") Long ansId)
    {
        OperationTransfer<AnswerInfo> operationTransfer;

        try {
            AnswerInfo answerInfo =  this.updateAnswerGood(ansId, GoodCount.PHRASE);
            operationTransfer = new OperationTransfer<AnswerInfo>(true, "成功点赞", answerInfo);
        } catch (Exception ex){
            operationTransfer = new OperationTransfer<AnswerInfo>(false, "点赞失败", null);
        }
        return operationTransfer;
    }

    @GET
    @Path("/{ansId}/disPhrase")
    @Produces(MediaType.APPLICATION_JSON)
    public OperationTransfer<AnswerInfo> disPhraseAnswer(@PathParam("ansId") Long ansId)
    {
        OperationTransfer<AnswerInfo> operationTransfer;

        try {
            AnswerInfo answerInfo =  this.updateAnswerGood(ansId, -GoodCount.PHRASE);
            operationTransfer = new OperationTransfer<AnswerInfo>(true, "取消成功", answerInfo);
        } catch (Exception ex){
            operationTransfer = new OperationTransfer<AnswerInfo>(false, "取消失败", null);
        }
        return operationTransfer;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/getAnswersByQueId/{queId: [0-9]*$}")
    public List<AnswerTransfer> getAnswersByQueId(@PathParam("queId")Long queId){
        List<AnswerTransfer> answerTransfers = new ArrayList<AnswerTransfer>();
//        AnswerInfo answerInfo = answerInfoDao.find()
        List<AnswerInfo> answerInfos = answerInfoDao.findByQueId(queId);
        if(answerInfos != null){
            for(AnswerInfo answerInfo : answerInfos){
                AnswerTransfer answerTransfer = new AnswerTransfer(true); //true 短的答案
                answerTransfer.setAnswerInfo(answerInfo);

                answerTransfer.setStudent(studentDao.find(answerInfo.getStuId()));

                answerTransfers.add(answerTransfer);
            }
        }
        return answerTransfers;
    }

    /**
     * 为问题加赞
     * @param queId
     * @param good
     */
    private QuestionInfo updateQuestionGood(Long queId, int good){
        QuestionInfo questionInfo = questionInfoDao.find(queId);
        if(questionInfo == null){
            return null;
        }

        questionInfo.setGoodCount(questionInfo.getGoodCount() + good);
        questionInfo = questionInfoDao.save(questionInfo);
        return questionInfo;
    }

    /**
     * 为答案和问题加赞
     * @param ansId
     * @param good
     */
    private AnswerInfo updateAnswerGood(Long ansId, int good){
        AnswerInfo answerInfo = answerInfoDao.find(ansId);
        if(answerInfo == null){
            return null;
        }

        answerInfo.setGoodCount(answerInfo.getGoodCount() + good);
        answerInfo = answerInfoDao.save(answerInfo);
        this.updateQuestionGood(answerInfo.getQueId(), good);
        return answerInfo;
    }
}